import request from '@/utils/request'

/**
 * 获取角色列表
 */
export function getRoleList(params: RoleQueryParams) {
  return request.get<PageResponse<RoleFormData>>('/roles', params)
}

/**
 * 获取所有角色（不分页）
 */
export function getAllRoles() {
  return request.get<RoleFormData[]>('/roles/all')
}

/**
 * 获取角色详情
 */
export function getRoleById(id: number) {
  return request.get<RoleFormData>(`/roles/${id}`)
}

/**
 * 创建角色
 */
export function createRole(data: RoleCreateRequest) {
  return request.post('/roles', data, {
    successMessage: '创建角色成功'
  })
}

/**
 * 更新角色
 */
export function updateRole(id: number, data: RoleUpdateRequest) {
  return request.put(`/roles/${id}`, data, {
    successMessage: '更新角色成功'
  })
}

/**
 * 删除角色
 */
export function deleteRole(id: number) {
  return request.delete(`/roles/${id}`, undefined, {
    successMessage: '删除角色成功'
  })
}

/**
 * 批量删除角色
 */
export function batchDeleteRoles(ids: number[]) {
  return request.delete(
    '/roles/batch',
    { ids },
    {
      successMessage: '批量删除成功'
    }
  )
}

/**
 * 分配资源权限
 */
export function assignResources(roleId: number, data: RoleResourceRequest) {
  return request.put(`/roles/${roleId}/resources`, data, {
    successMessage: '分配权限成功'
  })
}

/**
 * 获取角色的资源权限
 */
export function getRoleResources(roleId: number) {
  return request.get<number[]>(`/roles/${roleId}/resources`)
}
